package com.yunupmall.service.admin.impl;

import cn.hutool.core.util.ObjectUtil;
import com.yunupmall.api.po.MemberSexPo;
import com.yunupmall.api.po.UserCounts;
import com.yunupmall.entity.admin.memberpojo.Member;
import com.yunupmall.entity.admin.memberpojo.MemberSexCounts;
import com.yunupmall.entity.admin.memberpojo.Memberwhere;
import com.yunupmall.mapper.FordMapper.Membermapper;
import com.yunupmall.service.admin.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class Memberimpl implements MemberService {
    @Autowired
    Membermapper memberService;

    @Override
    public List<MemberSexPo> getmanCounts() {
        List<MemberSexCounts> memberSexCounts = memberService.getmanCounts();
        List<MemberSexPo> memberSexPoList = new ArrayList<>();
        memberSexCounts.forEach(memberSexCount->{
            MemberSexPo memberSexPo = new MemberSexPo();
            if (ObjectUtil.isEmpty(memberSexCount.getSex())){
                memberSexPo.setSex("未填写");
            }else {
                if (memberSexCount.getSex()==0){
                    memberSexPo.setSex("男");
                }
                if (memberSexCount.getSex() == 1) {
                    memberSexPo.setSex("女");
                }
            }
            memberSexPo.setCount(memberSexCount.getCounts());
            memberSexPoList.add(memberSexPo);
        });
        return memberSexPoList;
    }

    @Override
    public List<Member> queryAllMemberdata(Integer page, Integer size) {
        return memberService.queryAllMemberdata(page, size);
    }

    @Override
    public int querydatanum() {
        return memberService.querydatanum();
    }

    @Override
    public int updatememberStatics(Integer statics, Integer id) {
        int res = memberService.updatememberStatics(statics, id);
        return res;
    }

    @Override
    public List<Member> querywhereData(Memberwhere memberwhere) {
        return memberService.querywhereData(memberwhere);
    }

    @Override
    public int queryCounts() {
        return memberService.queryCounts();
    }

    /**
     * 获取当前年份中产生的数据
     * @return
     */
    @Override
    public List<UserCounts> getCounts(String date) {
        if (ObjectUtil.isEmpty(date)){
            int dayOfYear = LocalDateTime.now().getDayOfYear();
            date=String.valueOf(dayOfYear);
        }
        List<UserCounts> getcounts = memberService.getcounts(date);
        if (ObjectUtil.isNotEmpty(getcounts)){
            for (int i=1;i<=12;i++){
                String month=null;
                String finalMonth = month;
                int finalI = i;
                final Integer[] falg = {null};
                int finalI1 = i;
                getcounts.forEach(userCounts -> {
                    if (Objects.equals(userCounts.getMonth(), finalI1)){
                        falg[0]=10;
                    }
                });
                if (ObjectUtil.isEmpty(falg[0])){
                    UserCounts userCounts = new UserCounts();
                    userCounts.setMonth(i);
                    userCounts.setSum(0);
                    getcounts.add(userCounts);
                }
            }
            List<UserCounts> collect = getcounts.stream().sorted(Comparator.comparing(UserCounts::getMonth)).collect(Collectors.toList());
            return collect;
        }
        return null;
    }

}
